package tool.zyk.configuration;

import lombok.Getter;
import tool.zyk.connect.ServerConnectionFactory;
import tool.zyk.consts.RpcCommonConst;
import tool.zyk.consts.SerializableTypeEnum;
import tool.zyk.invoke.DefaultReflectExecutorFactory;
import tool.zyk.invoke.ReflectExecutorFactory;
import tool.zyk.utils.Asserts;
import tool.zyk.utils.ReflectUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * author: zyk
 * datetime: 2024/2/3 21:22
 * describe: 核心配置类
 */
@Getter
public class SimpleRpcConfiguration {
    private SimpleRpcConfiguration() {
    }

    public static SimpleRpcConfigurationBuilder builder() {
        return new SimpleRpcConfigurationBuilder();
    }

    private Integer serverPort;//服务端端口号

    private SerializableTypeEnum serializableType;//客户端协议类型

    private ReflectExecutorFactory executorFactory;//执行器工厂类

    private Long timeWaitMill;

    private ServerConnectionFactory serverConnectionFactory;

    private String applicationName;//服务名字

    //key为提供者的类名字
    //value为 提供者的应用名字
    private Map<String, String> providerCache;


    public static class SimpleRpcConfigurationBuilder {
        private SimpleRpcConfigurationBuilder() {
        }

        public SimpleRpcConfiguration build() {
            SimpleRpcConfiguration simpleRpcConfiguration = new SimpleRpcConfiguration();
            simpleRpcConfiguration.serverPort = Objects.isNull(serverPort) ? RpcCommonConst.DEFAULT_PORT : serverPort;
            simpleRpcConfiguration.serializableType = Objects.isNull(serializableType) ? SerializableTypeEnum.JSON : serializableType;
            simpleRpcConfiguration.executorFactory = Objects.isNull(executorFactoryClassname) ? new DefaultReflectExecutorFactory() : (ReflectExecutorFactory) ReflectUtils.getObjectByClassname(executorFactoryClassname);
            simpleRpcConfiguration.timeWaitMill = Objects.isNull(timeWaitMill) ? RpcCommonConst.DEFAULT_WAIT_TIME : timeWaitMill;
            Asserts.isNotNull(this.serverConnectionFactory, "服务连接工厂类不能为空");
            simpleRpcConfiguration.serverConnectionFactory = this.serverConnectionFactory;
            Asserts.isNotNull(this.applicationName, "服务名字不可为空");
            simpleRpcConfiguration.applicationName = this.applicationName;
            simpleRpcConfiguration.providerCache = new HashMap<>();
            return simpleRpcConfiguration;
        }

        private String applicationName;
        private Integer serverPort;
        private SerializableTypeEnum serializableType;

        private String executorFactoryClassname;

        private Long timeWaitMill;

        private ServerConnectionFactory serverConnectionFactory;

        public SimpleRpcConfigurationBuilder applicationName(String applicationName) {
            this.applicationName = applicationName;
            return this;
        }

        public SimpleRpcConfigurationBuilder serverConnectionFactory(ServerConnectionFactory serverConnectionFactory) {
            this.serverConnectionFactory = serverConnectionFactory;
            return this;
        }

        public SimpleRpcConfigurationBuilder timeWaitMill(Long timeWaitMill) {
            this.timeWaitMill = timeWaitMill;
            return this;
        }

        public SimpleRpcConfigurationBuilder executorFactoryClassname(String executorFactoryClassname) {
            this.executorFactoryClassname = executorFactoryClassname;
            return this;
        }


        public SimpleRpcConfigurationBuilder serializableType(SerializableTypeEnum serializableTypeEnum) {
            this.serializableType = serializableTypeEnum;
            return this;
        }

        public SimpleRpcConfigurationBuilder serverPort(int serverPort) {
            this.serverPort = serverPort;
            return this;
        }
    }

}

